package com.yueqian.dao;

import com.yueqian.domain.Permission;
import com.yueqian.domain.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IRoleDao {
    //通过用户id查询角色的集合(角色对象中包含权限的集合)
    @Select("select * from role r,users_role ur where r.id=ur.roleId and userid=#{uid}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "roleName", property = "roleName"),
            @Result(column = "roleDesc", property = "roleDesc"),
            @Result(column = "id", property = "permissions",
                    many = @Many(select = "com.yueqian.dao.IPermissionsDao.findByRoleId", fetchType = FetchType.LAZY))
    })
    List<Role> findRoleByUserId(String uid);

    @Select("select * from role")
    List<Role> findAll() throws Exception;

    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role) throws Exception;

    //根据角色id查找没有与之关联的所有权限
    @Select("select * from permission where id not in(select permissionid from role_permission where roleid=#{roleId})")
    List<Permission> findRoleAndNoPermission(String roleId) throws Exception;

    @Insert("insert into role_permission(roleid,permissionid) values(#{arg0},#{arg1})")
    void addPermissionToRole(String roleId, String permissionId) throws Exception;
}
